<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<title>建设阶段</title>
<base href="<%=basePath%>">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">

<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="js/bootstrap2/css/bootstrap.min.css">
<link rel="stylesheet" href="css/common.css">
<link rel="stylesheet" href="css/vivo.css">

<script src="js/jquery-1.11.1.js"></script>
<script type="text/javascript" src="js/layui-v2.1.5/layui/layui.all.js"></script>
<script type="text/javascript" src="js/form.js"></script>
<!-- 为了使用里面数组扩展indexof方法 -->
<script type="text/javascript" src="js/sharefunction.js"></script>
<script language="javascript" type="text/javascript"
	src="<%=basePath%>js/My97DatePicker/WdatePicker.js"></script>

<style type="text/css">
.style_btn {
	margin: 5px;
}
</style>
<script type="text/javascript">
	$(function(){
		mySubmit("buildStage", initBuildStage, successBuild);
		getBuildStage();
		//鼠标悬浮提示
		toolTip();
		//修改字段标记记录变量
		<c:set var="marks" value="原数据"/>
	});
	
	function getTip(data){
		var html = "<span class=\"toolTip mark-msg\"data-tip=\""+data+"\">原数据</span>"
		return html;
	}
	//获取建筑阶段
	function getBuildStage() {
		var data = ${bs};
		var manageMap = ${requestScope.manageMap};
		var defautHtml = "<span class=\"toolTip mark-msg\"data-tip=\"空\">原数据</span>";
		$("#buildStage input[name='buildDate']").val(new Date(data.buildDate).format('yyyy-MM-dd'));
		$("#buildStage input[name='buildName']").val(data.buildName);
		$("#buildStage input[name='buildName']").attr("data-tip",data.buildName);
		$("#buildStage input[name='buildUnit']").val(data.buildUnit);
		$("#buildStage input[name='buildUnit']").attr("data-tip",data.buildUnit);
		$("#buildStage input[name='allJobUnit']").val(data.allJobUnit);
		$("#buildStage input[name='allJobScope']").val(data.allJobScope);
		$("#buildStage input[name='projectAssociate']").val(data.projectAssociate);
		$("#buildStage input[name='businessUnit']").val(data.businessUnit);
		$("#buildStage input[name='superviseUnit1']").val(data.superviseUnit);
		$("#buildStage input[name='superviseUnit1']").attr("data-tip",data.superviseUnit);
		$("#buildStage input[name='subpackageId']").val(data.subpackageId);
		$("#buildStage input[name='superviseContact']").val(data.superviseContact);
		$("#buildStage input[name='superviseTel']").val(data.superviseTel);
		$("#buildStage input[name='buId']").val(data.id);
		//添加总包单位列表
		if(data.manageList!=null){
			for(var a=0;a<data.manageList.length;a++){
				addDivManageUnit();
			}
			$manage = $("#div_build").find(".manageUnit");
			for(var i=0;i<data.manageList.length;i++){
				m=$manage[i];
				$(m).find("input[name='id']").val(data.manageList[i].id);
				$(m).find("input[name='allJobUnit']").val(data.manageList[i].allJobUnit);
				$(m).find("input[name='allJobUnit']").attr("data-tip",data.manageList[i].allJobUnit);
				$(m).find("input[name='allJobScope']").val(data.manageList[i].allJobScope);
				$(m).find("input[name='allJobScope']").attr("data-tip",data.manageList[i].allJobScope);
				$(m).find("input[name='contacts']").val(data.manageList[i].contacts);
				$(m).find("input[name='contactNumber']").val(data.manageList[i].contactNumber); 
				var manMap = manageMap[data.manageList[i].id];
				//添加总包数据对比
				if(manMap != undefined){
					if(manMap.allJobUnit != undefined && manMap.allJobUnit != ''){
						$(m).find("input[name='allJobUnit']").parent().append(getTip(manMap.allJobUnit));
					}else if(manMap.allJobUnit == ''){
						$(m).find("input[name='allJobUnit']").parent().append(defautHtml);
					}
					if(manMap.allJobScope != undefined && manMap.allJobScope != ''){
						$(m).find("input[name='allJobScope']").parent().append(getTip(manMap.allJobScope));
					}else if(manMap.allJobScope == '' && manMap.allJobUnit != ''){
						$(m).find("input[name='allJobScope']").parent().append(defautHtml);
					}
					if(manMap.contacts != undefined && manMap.contacts != ''){
						$(m).find("input[name='contacts']").parent().append(getTip(manMap.contacts));
					}else if(manMap.contacts == ''){
						$(m).find("input[name='contacts']").parent().append(defautHtml);
					}
					if(manMap.contactNumber != undefined && manMap.contactNumber != ''){
						$(m).find("input[name='contactNumber']").parent().append(getTip(manMap.contactNumber));
					}else if(manMap.contactNumber == ''){
						$(m).find("input[name='contactNumber']").parent().append(defautHtml);
					}
				}
				 //添加分包div模板并赋值
				 var subList=data.manageList[i].subUnitList;//
				 var subMaps;
				 if(manMap != undefined){
					 subMaps = manMap.subUnitList;
				 }
				 if(subList!=null && subList.length !=0){
					for(var s=0;s<subList.length;s++){
					$(m).find("a[name='addbtn']").click();
						//addDiv2();
					}
					$t = $(m).parent().find("#div_sub_build table");
					for(var j=0;j<subList.length;j++){
						v=$t[j];
						$(v).find("input[name='superviseUnit']").val(subList[j].superviseUnit);
						$(v).find("input[name='superviseUnit']").attr("data-tip",subList[j].superviseUnit);
						$(v).find("input[name='businessUnit']").val(subList[j].businessUnit);
						$(v).find("input[name='businessUnit']").attr("data-tip",subList[j].businessUnit);
						$(v).find("input[name='unitName']").val(subList[j].unitName);
						$(v).find("input[name='principalTel']").val(subList[j].principalTel);
						$(v).find("textarea[name='remark']").val(subList[j].remark);
						$(v).find("input[name='id']").val(subList[j].id);
						//添加分包数据对比
						var subMap = subMaps[subList[j].id];
						if(subMap!= undefined){
							if(subMap.superviseUnit != undefined && subMap.superviseUnit != ''){
								$(v).find("input[name='superviseUnit']").parent().append(getTip(subMap.superviseUnit));
							}else if(subMap.superviseUnit == ''){
								$(v).find("input[name='superviseUnit']").parent().append(defautHtml);
							}  
							if(subMap.businessUnit != undefined && subMap.businessUnit != ''){
								$(v).find("input[name='businessUnit']").parent().append(getTip(subMap.businessUnit));
							}else if(subMap.businessUnit == ''){
								$(v).find("input[name='businessUnit']").parent().append(defautHtml);
							}
							if(subMap.unitName != undefined && subMap.unitName != ''){
								$(v).find("input[name='unitName']").parent().append(getTip(subMap.unitName));
							}else if(subMap.unitName == ''){
								$(v).find("input[name='unitName']").parent().append(defautHtml);
							}
							if(subMap.principalTel != undefined && subMap.principalTel != ''){
								$(v).find("input[name='principalTel']").parent().append(getTip(subMap.principalTel));
							}else if(subMap.principalTel == ''){
								$(v).find("input[name='principalTel']").parent().append(defautHtml);
							}
							if(subMap.remark != undefined && subMap.remark != ''){
								$(v).find("input[name='remark']").parent().append(getTip(subMap.remark));
							}else if(subMap.remark == ''){
								$(v).find("input[name='remark']").parent().append(defautHtml);
							}
						}
					}
				}  
			}
		}
		//隐藏按钮
		$("#buildStage #btn_div2").show();
		$("#buildStage #btn_div1").hide();
		$("#buildStage").find("input,textarea:not([name=approvalRemark])").attr("readonly",true);
		$("#buildStage").find(".addbtn,.Wdate").attr("disabled",true);
		$("#buildStage").find(".icon-fullscreen").hide();
			
	}  
	
		var optBuildStage;
		//optPlanStage操作字段，0：通过 1 拒绝
		function saveBuildStage(val){
			optBuildStage = val;
			$("#buildStage").submit();
		}
</script>
</head>
<body>
	<form class="cmxform" id="buildStage" method="post"
		action="project/approval/disposeBuild">
		<div style="padding:0px 10px">
			<div class="panel panel-default">
				<div class="panel-heading">
					<h5 class="panel-title" style="font-weight: bold;">建设阶段</h5>
				</div>
				<div class="panel-body">
					<input type="hidden" name="buId" id="entityId_buId"/><!-- 实体id -->
					<table cellpadding="10" cellspacing="5" class="mytable">
						<tr>
							<td width="70">建设时间</td>
							<td width="260"><input name="buildDate" id="buildDate" readonly="readonly" class="Wdate" onClick="WdatePicker()" />
								<c:if test="${not empty requestScope.map.buildDate }"><span class="toolTip mark-msg" data-tip="<fmt:formatDate  value="${requestScope.map.buildDate }" type="both" pattern="yyyy-MM-dd" />">
								<c:out value="${marks}"/></span>
							</c:if>
							<c:if test="${requestScope.map.buildDate=='' }"><span class="toolTip mark-msg"data-tip="空">
								<c:out value="${marks}"/></span>
							</c:if>
							</td>
							<td width="70">建设名称</td>
							<td><input name="buildName" class="toolTip" value="" />
								<c:if test="${not empty requestScope.map.buildName }"><span class="toolTip mark-msg"data-tip="${requestScope.map.buildName }">
									<c:out value="${marks}"/></span>
								</c:if>
								<c:if test="${requestScope.map.buildName=='' }"><span class="toolTip mark-msg"data-tip="空">
									<c:out value="${marks}"/></span>
								</c:if>
							</td>
							<td width="70">建设单位</td>
							<td><input name="buildUnit" value="" class="input_name toolTip"/>
								<c:if test="${not empty requestScope.map.buildUnit }"><span class="toolTip mark-msg"data-tip="${requestScope.map.buildUnit }">
									<c:out value="${marks}"/></span>
								</c:if>
								<c:if test="${requestScope.map.buildUnit=='' }"><span class="toolTip mark-msg"data-tip="空">
									<c:out value="${marks}"/></span>
								</c:if>
							</td>
						</tr>
					</table>
			       	<!-- <h4>施工单位</h4>  -->
			       	<div><span class="style_btn">施工单位</span> <a class="btn btn-danger radius a_font addbtn" onclick="addDivManageUnit()">添加总包</a></div>
					<!-- 动态div start -->
					<div id="div_build"></div>
					<!-- 动态div end-->
					<p style="margin:10px 0px 0px 0px;">
						<span>监理单位</span> <span style="margin:0px 105px 0 10px;"><input value="" name="superviseUnit1" />
							<c:if test="${not empty requestScope.map.superviseUnit1 }"><span class="toolTip mark-msg"data-tip="${requestScope.map.superviseUnit1 }">
								<c:out value="${marks}"/></span>
							</c:if>
							<c:if test="${requestScope.map.superviseUnit1=='' }"><span class="toolTip mark-msg"data-tip="空">
								<c:out value="${marks}"/></span>
							</c:if>
						</span>
						<span>联系人</span> <span style="margin:0px 70px 0 10px;"><input value="" name="superviseContact" />
							<c:if test="${not empty requestScope.map.superviseContact }"><span class="toolTip mark-msg"data-tip="${requestScope.map.superviseContact }">
								<c:out value="${marks}"/></span>
							</c:if>
							<c:if test="${requestScope.map.superviseContact=='' }"><span class="toolTip mark-msg"data-tip="空">
								<c:out value="${marks}"/></span>
							</c:if>
						</span>
						<span>联系电话</span> <span style="margin:0px 0px 0 10px;"><input value="" name="superviseTel" /> 
							<c:if test="${not empty requestScope.map.superviseTel }"><span class="toolTip mark-msg"data-tip="${requestScope.map.superviseTel }">
								<c:out value="${marks}"/></span>
							</c:if>
							<c:if test="${requestScope.map.superviseTel=='' }"><span class="toolTip mark-msg"data-tip="空">
								<c:out value="${marks}"/></span>
							</c:if>
						</span>
					</p>
				</div>
			</div>
			<input type="hidden" id="entityId_id" name="buId" value=""/>
			<input type="hidden" id="apId" name="apId" value="${requestScope.apId }"/>
			<c:if test="${requestScope.notPassExamine}">
				<!-- 修改时添加变更说明 -->
				<div style="margin:10px;" id="approvalRemark">
					审批意见<textarea rows="3" name="approvalRemark"style="width:70%"></textarea><span style="color:red;">*</span>
				</div>
				<div style="width:100%;text-align:center; padding-top:0px">
					<button class="btn btn-danger radius my-btn"
						onclick="saveBuildStage(0)" type="button">同意</button>
					<button class="btn btn-default radius"
						onclick="saveBuildStage(1)" type="button">拒绝</button>
				</div>
			</c:if>
		</div>
	</form>

	<script type="text/javascript">
		function addDiv2(obj) {
			var node;
			node = $("#build_templete>div").clone(true);
			var oTest = $(obj).parent().parent().parent().parent().parent().find("#div_sub_build");
			oTest.append(node);
		}
		//添加总包单位
		function addDivManageUnit() {
			var node;
			node = $("#manage_templete>div").clone(true);
			var oTest = $("#div_build");
			oTest.append(node);
		}
		
		
		var i;
		//封装表单数据为json
		function initBuildStage() {
			i = layer.load(2);
			var subpackageId = $("#entityId_subpackageId").val();
			//封装基础信息
			var id = $("input[name='buId']").val();
			var buildDate = $("input[name='buildDate']").val();
			var buildName = $("input[name='buildName']").val();
			var buildUnit = $("input[name='buildUnit']").val();
			var superviseUnit = $("input[name='superviseUnit1']").val();
			var superviseContact = $("input[name='superviseContact']").val();
			var superviseTel = $("input[name='superviseTel']").val();
			var fkPa = window.parent.$("#fkPa").val();
			var designId = parent.$("#entityId_designId").val();
			var approvalRemark = $("#buildStage textarea[name='approvalRemark']").val();
			var delIds = '${requestScope.delIds}';
			var delManegeIds = '${requestScope.delManageIds}';
			var apId = $("#buildStage #apId").val();
			//封装总包单位信息
			var manageUnits = $("#div_build").find(".manageUnit");
			var a;
			var manageList = [];//总包单位列表
			if (manageUnits.length > 0) {
				for (var i = 0; i < manageUnits.length; i++) {
					a = packageUnitMsg(manageUnits[i]);//封装总包单位数据
					if (a == null) {
						return null;
					} else {
						manageList.push(a);
					}
				}
			}
			
			if(id!=""){
				if(approvalRemark==""){
					layer.alert("变更说明不能为空");
					layer.close(i);
					return null;
				}
			}
			if (buildDate == "") {
				layer.alert("建设时间不能为空");
				layer.close(i);
				return null;
			}
			else {
				return {
					id:id,
					fkProjectApproval : fkPa,
					buildDate : buildDate,
					buildName : buildName,
					buildUnit : buildUnit,
					superviseUnit : superviseUnit,
					superviseContact:superviseContact,
					superviseTel:superviseTel,
					opt:optBuildStage,
					delIds : delIds,
					designId:designId,
					approvalRemark:approvalRemark,
					manageList:manageList,
					delManegeIds:delManegeIds,
					apId:apId
				};
	
			}
	
		}
		//封装总包单位信息
		function packageUnitMsg($unit){
			x=layer.load(2);
			$unit = $($unit);
			var fkBuildstage = $("#entityId_subpackageId").val();
			var manageId=$unit.find("input[name='id']").val();
			var allJobUnit = $unit.find("input[name='allJobUnit']").val();
			var allJobScope = $unit.find("input[name='allJobScope']").val();
			var contacts = $unit.find("input[name='contacts']").val();
			var contactNumber = $unit.find("input[name='contactNumber']").val(); 
			//封装分包单位数据
			var $t = $unit.parent().find("#div_sub_build .subUnit");
			var build = new Array();
			//用来记录分包单位名称，以判断是否重复
			var soleArr=new Array();
			var b=true;//分包单位如果重复该值会为false
			$.each($t, function(i, v) {
				var superviseUnit = $(v).find("input[name='superviseUnit']").val();
				if(soleArr.indexOf(superviseUnit)!=-1){//判断分包单位名称是否重复
					b=false;
					return b;
				}
				soleArr.push(superviseUnit);
				var businessUnit = $(v).find("input[name='businessUnit']").val();
				var unitName = $(v).find("input[name='unitName']").val();
				var principalTel = $(v).find("input[name='principalTel']").val();
				var remark = $(v).find("textarea[name='remark']").val();
				var id = $(v).find("input[name='id']").val();
					
				if(remark.length>200){
					layer.msg("备注不能超过200字");
					$(v).find("textarea[name='remark']").focus();
					layer.close(i);
					return null;
				}
				build.push({
					id:id,
					superviseUnit : superviseUnit,
					businessUnit : businessUnit,
					unitName : unitName,
					//unitTel : unitTel,
					principalTel : principalTel,
					remark : remark,
					fkSubpackageId:manageId
				});
			});
			if (allJobUnit == "") {
				layer.alert("总包单位不能为空");
				layer.close(i);
				return null;
			}else if(allJobUnit.length>20){
				layer.alert("总包单位不能超过50字");
				layer.close(i);
				return null;
			}else if(!b){
				layer.alert("分包单位不能重复");
				layer.close(i);
				return null;
			}else{
				return {
					id : manageId,
					allJobUnit : allJobUnit,
					allJobScope : allJobScope,
					contacts : contacts,
					contactNumber : contactNumber,
					fkBuildstage : fkBuildstage,
					subUnitList:build
				};
			}
		}
	
		//成功后调用方法
		function successBuild(responseText) {
			layer.close(i);
			if (responseText.success) {
				layer.alert("保存成功！", {
					offset:'20%',
					shade:[0.1,'#fff']
				}, function() {
					parent.location.reload();
					parent.parent.getPendingCount();
					parent.parent.getPendingPassCount();
					parent.parent.getPendingBackCount();
					layer.close(i);
				});
			} else {
				alert(responseText.msg, {icon : 7});
				layer.closeAll();
			}
		}
	</script>
	
	<!-- 总包单位模板div start -->
	<div id="manage_templete" style="display:none;">
		<div class="style_border_tem">
			<table cellpadding="10" cellspacing="5" class="mytable manageUnit">
				<tr>
					<td width="90"><span class="style_btn">总包单位</span><input type="hidden" id="entityId_id" name="id" value="" />
					</td>
					<td width="320"><input name="allJobUnit" class="toolTip" value="" />
						
					</td>
					<td width="90"><span class="style_btn">总包范围</span></td>
					<td><input name="allJobScope" value="" />
					</td>
					<td  valign="top" align="right"><a
						href="javascript:void(0)" onclick="delTrDivManage(this)"> <i
							class="icon-fullscreen"></i>
					</a></td>
				</tr>
				<tr>
					<td><span class="style_btn">联系人</span></td>
					<td><input name="contacts" value="" /></td>
					<td><span class="style_btn">联系人电话</span></td>
					<td colspan="2"><input name="contactNumber" style="margin-right:15px;" value="" />
						<a class="btn btn-danger radius a_font addbtn" name="addbtn" onclick="addDiv2(this)">
							添加分包</a></td>
				</tr>
			</table>
			<!-- 动态div start -->
			<div id="div_sub_build"></div>
			<!-- 动态div end-->
		</div>
	</div>
	<!-- 模板div start -->
	<div id="build_templete" style="display:none;">
		<div class="style_border_tem">
			<table cellpadding="10" cellspacing="5" class="mytable subUnit">
				<tr>
					<td width="90"><span class="style_btn">分包单位</span><input type="hidden" id="entityId_id" name="id" value="" /></td>
					<td><input id="" class="toolTip" name="superviseUnit" value="" />
					</td>
					<td width="90"><span class="style_btn">分包范围</span></td>
					<td><input id="" name="businessUnit" value="" />
					</td>
					<td  valign="top" align="right"><a
						href="javascript:void(0)" onclick="delTrDivBuild(this)"> <i
							class="icon-fullscreen"></i>
					</a></td>
				</tr>
				<!-- <tr>
					<td width="90"><span>业务单位</span></td>
					<td width="320"><input id="" name="businessUnit" value="" /></td>
					<td width="90"><span>单位名称</span></td>
					<td><input id="" name="unitTel" value="" /></td>
				</tr> -->
				<tr>
					<td width="90"><span class="style_btn">联系人</span></td>
					<td width="320"><input id="" name="unitName" value="" /></td>
					<td width="90"><span class="style_btn">联系人电话</span></td>
					<td><input id="" name="principalTel" value="" /></td>
				</tr>
				<tr>
					<td><span class="style_btn">备注</span></td>
					<td colspan="3"><textarea name="remark" rows="3" cols="70" style="width:70%;" maxlength="200" onkeydown="checkMaxInput(this,200)"  
            						onkeyup="checkMaxInput(this,200)" onfocus="checkMaxInput(this,200)" onblur="checkMaxInput(this,200);resetMaxmsg()"></textarea></td>
				</tr>
			</table>
		</div>
	</div>
	<!-- 模板div end -->
</body>
</html>

